//
//  MineAddressNewViewController.m
//  shopping
//
//  Created by 马田成 on 2017/9/1.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "MineAddressNewViewController.h"
#import "AddressPickerView.h"
#import "MineShengCityViewController.h"


@interface MineAddressNewViewController ()<addressDelegate>
{
  
    UIView *grayView;
    NSString *addressNew;
    NSString *addressCityID;//城市id
    NSString *addressQuID; //地区ID
    NSString *addressShenID;//省id
    
}

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *jieDaoTF;
@property (weak, nonatomic) IBOutlet UIButton *shengbTN;
@property (weak, nonatomic) IBOutlet UIButton *chengShi;
@property (weak, nonatomic) IBOutlet UIButton *diQu;

@end

@implementation MineAddressNewViewController

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"添加收货地址"];
    if (self.name.length == 0) {
        
    }else {
        
        self.nameTF.text = self.name;
        self.phoneTF.text = self.phone;
        self.jieDaoTF.text = self.dizhi;
        [self.shengbTN setTitle:self.sheng forState:UIControlStateNormal];
        [self.chengShi setTitle:self.shi forState:UIControlStateNormal];
        [self.diQu setTitle:self.qu forState:UIControlStateNormal];
    }

    // Do any additional setup after loading the view from its nib.
}

- (void)withString:(NSString *)address addressID:(NSString *)addressID type:(NSString *)type{

    
    if ([type isEqualToString:@"1"]) {
        
        [self.shengbTN setTitle:address forState:UIControlStateNormal];
        
        addressShenID = addressID;
    }else if([type isEqualToString:@"2"]){
    
        addressCityID = addressID;

        [self.chengShi setTitle:address forState:UIControlStateNormal];
        
    }else {
    
        addressQuID = addressID;
        [self.diQu setTitle:address forState:UIControlStateNormal];
        
    }
    
   
    
    
    
}
//省
- (IBAction)shengBtn:(UIButton *)sender {
    MineShengCityViewController *vc = [[MineShengCityViewController alloc] init];
    vc.type = @"1";
    vc .delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    
}

//城市
- (IBAction)chengShiClick:(UIButton *)sender {
    
    MineShengCityViewController *vc = [[MineShengCityViewController alloc] init];
    vc.delegate = self;
    vc.type = @"2";
    NSString *str = [NSString stringWithFormat:@"%@", addressShenID];
    
    if (str.length < 4) {
        [PublicFunction showAlert:nil message:@"请选择省份" duration:10.0f];
    }else {
    
        
        vc.provinceId = addressShenID;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

//区
- (IBAction)diQuClick:(UIButton *)sender {
    
    MineShengCityViewController *vc = [[MineShengCityViewController alloc] init];
    vc.delegate = self;
    vc.type = @"3";
    NSString *str = [NSString stringWithFormat:@"%@", addressCityID];

    if (str.length < 4) {
        
        [PublicFunction showAlert:nil message:@"请选择城市" duration:10.0f];
    }else {
    
        
        vc.quID = addressCityID;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    
}
//添加
- (IBAction)tianjiaNewAdd:(UIButton *)sender {
    
    
    if ([self.type isEqualToString:@"1"]) {
        
        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        NSString *token = [PublicFunction getConfigWithKey:@"token"];
        //  NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
        
        [productParams setValue:token forKey:@"token"];
        [productParams setValue:self.nameTF.text forKey:@"userName"];
        [productParams setValue:self.phoneTF.text forKey:@"userTel"];
        [productParams setValue:self.jieDaoTF.text forKey:@"address"];
        [productParams setValue:addressShenID forKey:@"areaId1"];
        [productParams setValue:addressCityID forKey:@"areaId2"];
        [productParams setValue:addressQuID forKey:@"areaId3"];
        [productParams setValue:self.phoneTF.text forKey:@"userPhone"];
        
        
        [productParams setValue:@"1" forKey:@"type"];
        [[MNetworkHandler sharedInstance]conURL:adminOrderaddUserAddress networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
            
            
            if ([[returnData objectForKey:@"status"]intValue]==1) {
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
            }
            
            else {
                
                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
//            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
            
        }];

    }else {
    
        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        NSString *token = [PublicFunction getConfigWithKey:@"token"];
        //  NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
        
        [productParams setValue:token forKey:@"token"];
        [productParams setValue:self.nameTF.text forKey:@"userName"];
        [productParams setValue:self.phoneTF.text forKey:@"userTel"];
        [productParams setValue:self.jieDaoTF.text forKey:@"address"];
        [productParams setValue:addressShenID forKey:@"areaId1"];
        [productParams setValue:addressCityID forKey:@"areaId2"];
        [productParams setValue:addressQuID forKey:@"areaId3"];
        [productParams setValue:self.phoneTF.text forKey:@"userPhone"];
        [productParams setValue:self.dizhiID forKey:@"id"];
        
        [productParams setValue:@"2" forKey:@"type"];
        [[MNetworkHandler sharedInstance]conURL:adminOrderaddUserAddress networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
            
            
            if ([[returnData objectForKey:@"status"]intValue]==1) {
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
            }
            
            else {
                
                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
//            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
            
        }];

    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
